package com.project.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.project.entity.Idea;
import com.project.entity.Project;
import com.project.entity.User;
import com.project.util.DBHelp;
import com.project.util.RowMapper;

public class IdeaDao {
	DBHelp<Idea> db=new DBHelp<Idea>();
	public List<Idea> findAllIdeaByProjectid(String projectid) {
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT t_idea.id,t_idea.name,t_idea.cont,t_idea.createtime,t_idea.userid,t_idea.projectid,t_user.username AS username FROM t_idea    ");
		sql.append("LEFT JOIN t_user ON t_user.id=t_idea.userid  ");
		sql.append(" WHERE projectid= ? ");
		return db.executeQueryForList(sql.toString(), new ideaRowMapper(), projectid);
		
	
	}
	

	private class ideaRowMapper implements RowMapper<Idea>{

		public Idea mapRow(ResultSet rs) throws SQLException {
			Idea idea=new Idea();
			User user =new User();
			user.setId(rs.getString("userid"));
			user.setUsername(rs.getString("username"));
			idea.setUser(user);
			
			

			/*Project p = new Project();
			p.setCreatetime(rs.getString("createtime"));
			p.setCont(rs.getString("cont"));
			p.setId(rs.getString("projectid"));
			p.setName(rs.getString("name"));
			idea.setProject(p);*/
			
			
			
 			idea.setCont(rs.getString("cont"));
 			idea.setCreatetime(rs.getString("createtime"));
 			idea.setId(rs.getString("id"));
 			idea.setName(rs.getString("name"));
 			idea.setProjectid(rs.getString("projectid"));
 			idea.setUserid(rs.getString("userid"));
 			
 			
			return idea;
		}
		
	}


	public void save(Idea i) {
		
		String sql="insert into t_idea (id,name,cont,createtime,userid,projectid)values(?,?,?,?,?,?) " ;
		db.executeSQL(sql, i.getId(),i.getName(),i.getCont(),i.getCreatetime(),i.getUserid(),i.getProjectid());
		
		
	}


	public Idea findIdeaById(String id2) {
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT t_idea.id,t_idea.name,t_idea.cont,t_idea.createtime,t_idea.userid,t_idea.projectid,t_user.username AS username FROM t_idea    ");
		sql.append("LEFT JOIN t_user ON t_user.id=t_idea.userid  ");
		sql.append(" WHERE t_idea.id= ? ");
		
		return db.executeQueryForObject(sql.toString(), new ideaRowMapper(), id2);
	}


	public void update(Idea idea) {
		String sql="update t_idea set name=?, cont=?, createtime=? where id=?";
		db.executeSQL(sql, idea.getName(),idea.getCont(),idea.getCreatetime(),idea.getId());
	}


	public void delIdeaById(String id2) {
		String sql="delete from t_idea where id=?";
		db.executeSQL(sql, id2);
	}


	
	
}
